package com.itheima.controller;

import com.itheima.common.JsonResult;
import com.itheima.admin.dishpojo.Dish;

import com.itheima.admin.AdminPage;
import com.itheima.service.DishService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author wuzhiyong
 */
@RestController
@RequestMapping(value = "/dish")
public class DishController {
    @Autowired
    private DishService dishService;

    /**
     * 分页查询
     *
     * @param name     name
     * @param page     page
     * @param pagesize pagesize
     * @param type     type
     * @return JsonResult
     */
    @GetMapping(value = "/page")
    public JsonResult<AdminPage<Dish>> dishPageDish(@RequestParam(value = "name", required = false) String name, @RequestParam(value = "page") Integer page, @RequestParam(value = "pageSize") Integer pagesize, @RequestParam(value = "type", required = false) Integer type) {
        AdminPage<Dish> selectpagedh = dishService.getPageDish(name, page, pagesize, type);
        return JsonResult.success(selectpagedh, null);
    }

    /**
     * 菜品下拉列表
     *
     * @param categoryId categoryId
     * @return List<Dish>
     */
    @GetMapping(value = "/list")
    public JsonResult<List<Dish>> dishList(@RequestParam(value = "categoryId", required = false) String categoryId) {
        List<Dish> dishes = dishService.getDishByCategoryId(categoryId);
        if (dishes != null) {
            return JsonResult.success(dishes, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 根据id查询
     *
     * @param id id
     * @return Dish
     */
    @GetMapping("/{id}")
    public JsonResult<Dish> dishId(@PathVariable("id") String id) {
        Dish dish = dishService.getDishtByDishId(id);
        if (dish != null) {
            return JsonResult.success(dish, null);
        }
        return JsonResult.error(null);
    }

    /**
     * 新增
     *
     * @param dish dish
     * @return JsonResult
     */
    @PostMapping
    public JsonResult<Object> dishAdd(@RequestBody Dish dish) {
        return dishService.addDish(dish) ? JsonResult.success("新增菜品成功", null) : JsonResult.error(null);
    }

    /**
     * 修改
     *
     * @param dish dish
     * @return JsonResult
     */
    @PutMapping
    public JsonResult<Object> dishModify(@RequestBody Dish dish) {
        return dishService.modifyDish(dish)?JsonResult.success("修改菜品成功", null):JsonResult.error(null);
    }

    /**
     * 批量删除
     *
     * @param ids ids
     * @return JsonResult
     */
    @DeleteMapping
    public JsonResult<String> dishRemove(@RequestParam(value = "ids") String[] ids) {
        return dishService.removeByIds(ids)?JsonResult.success("成功", null):JsonResult.error("失败");
    }

    /**
     * 批量启停售
     *
     * @param status status
     * @param ids ids
     * @return JsonResult
     */
    @PostMapping(value = "/status/{status}")
    public JsonResult<String> dishStatus(@PathVariable(value = "status", required = false) Integer status, @RequestParam(value = "ids") String[] ids) {
        return dishService.modifyByStatus(status, ids)?JsonResult.success("成功", null):JsonResult.error("失败");
    }

}

